import React from 'react';

import {
  View,
  Text,
  Image,
  TouchableOpacity,
} from 'react-native';

import ViewPager from 'react-native-viewpager';

import {Button} from 'react-native-elements';

import Config from '../../config';
import SizeUtils from '../../common/SizeUtils';
import BasePage from '../common/BasePage';
import Platform from 'Platform';
import TImage from '../../components/TImage';
import CommonRequestSliderImagesAction from '../../actions/common/CommonRequestSliderImagesAction';
import CommonCheckNeedGoNotificationAction from '../../actions/common/CommonCheckNeedGoNotificationAction';
import JumpToPageByUrlAction from '../../actions/common/JumpToPageByUrlAction';

export default class LoginAdvertPage extends BasePage {
  constructor(props) {
    super(props);

    this._imageHeight = SizeUtils.getWindowSize().height;

    this.state.data = [];
    this.state.requestBack = false;
    this._timer = 0;
    this.isGotoHomePage = false;
    this._goNotificationTimer = 0;
  }

  componentDidMount() {
    this._laterCall();
    this._requestData();
    // this._initGoNotificationTimer();
  }

  _initGoNotificationTimer() {
    clearTimeout(this._goNotificationTimer);
    this._goNotificationTimer = setTimeout(()=>{
     clearTimeout(this._goNotificationTimer);
    //  new CommonCheckNeedGoNotificationAction().call(this);
   }, 1000);
  }

  _requestData() {
    new CommonRequestSliderImagesAction().call(this, {tag:10});  //tag == 10
  }

  requestBack(data){
    if(data != null && data.length == 0)
    {
      this._onPressEnterBtn();
    }
  }

  render() {
    return (
      <View style={{width:SizeUtils.getWindowSize().width,
        height:SizeUtils.getWindowSize().height }}>
        {this._renderImg()}
        {this._renderButton()}
      </View>
    );
  }

  _laterCall(){
    clearTimeout(this._timer);
    this._timer = setTimeout(()=>{
     clearTimeout(this._timer);
     this._onPressEnterBtn();
   }, 5000);
  }

  _dealEnterPage() {
    clearTimeout(this._timer);
  }

  _dealLeavePage() {
    clearTimeout(this._timer);
  }

  _renderImg(){
    if(this.state.data != null && this.state.data.length > 0)
    {
      return(
        <TouchableOpacity onPress={() => this._pressImg()}>
          <View style={{}}>
            <TImage source={{uri:this.state.data[0].image_url}}
              resizeMode="contain"
              style={{width:SizeUtils.getWindowSize().width, height:this._imageHeight}}
              translatedSize={true} />
          </View>
        </TouchableOpacity>
      )
    }
  }

  _pressImg(){
    if(this.state.data != null && this.state.data.length > 0 && this.state.data[0].link != "") {
      this.gotoPage(Config.pages.home);
      new JumpToPageByUrlAction().call(this, this.state.data[0]);
    }
  }

  _renderButton() {
      if(this.state.data != null && this.state.data.length > 0)
      {
      return (
        <View style={{
            position:"absolute", bottom:SizeUtils.translateDesignHeightToCode(20),
            right:SizeUtils.translateDesignWidthToCode(20),
            // left:SizeUtils.translateDesignWidthToCode(356)
          }}>
          <Button
            backgroundColor="#000000cc"
            disabled = {this.state.userNo == "" || this.state.password == ""}
            buttonStyle = {{width: SizeUtils.translateDesignWidthToCode(120), height:SizeUtils.translateDesignHeightToCode(60),
              borderRadius:30, }}
            textStyle={{fontSize: SizeUtils.translateFontSizeToCode(30), fontWeight:"bold"}}
            title = "跳过"
            onPress = {() => this._onPressEnterBtn()}
          />
        </View>
      )
    }
  }

  _onPressEnterBtn() {
    if(this.props.pageData.routeData.authorization) {
      if(this.props.pageData.routeData.authorization != null && this.props.pageData.routeData.authorization != "0")
      {
        this.gotoPage(Config.pages.home);
      }
      else{
         this.gotoPage(Config.pages.login);
      }
    }
    else {
      this.gotoPage(Config.pages.login);
    }
  }
}
